package com.itheima.dao;

import com.github.pagehelper.Page;
import com.itheima.pojo.Menu;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select;
import org.apache.ibatis.annotations.Update;

import java.util.ArrayList;
import java.util.List;

public interface MenuDao {
    Page<Menu> findAllMenu(String queryString);

    List<Menu> findAll();


    void add(Menu menu);

    Menu findById(Integer id);

    void edit(Menu menu);

    void delete(Integer id);

    Integer findAndMenuAssiation(Integer id);

    void deleteAndRoleAssiation(Integer id);

    ArrayList<Menu> findMenuparentMenuId();

    ArrayList<Menu> findMenuparentMenuIdDisabled(Integer id);

    Menu findDisabled(Integer cid);


    Integer findLevel(Integer id);

    List<Integer> findLevel2Children(Integer id);

    List<Integer> findLevel1Children(Integer id);

    @Update("UPDATE t_menu  SET priority = priority+2 WHERE LEVEL = #{level} AND priority > #{priority} ")
    void updatePriority(@Param("level") Integer level,@Param("priority") Integer priority);

    @Select("<script>" +
            "select path from t_menu " +
            "where level = #{level}  " +
            "<if test=\"pid == null\"> and parentMenuId is null</if> " +
            "<if test=\"pid != null\"> and parentMenuId = #{pid}</if> " +
            " ORDER BY LENGTH(path) DESC, path  DESC limit 1"+
            "</script>")
    String findLastPath(@Param("level")Integer level, @Param("pid")Integer parentMenuId);

    @Select("SELECT * FROM t_menu a WHERE a.`id` = (SELECT  b.`parentMenuId` FROM t_menu b WHERE b.`id` = #{id} )")
    Menu findParentMenuById(Integer id);

    @Select("<script>" +
            "select * from t_menu " +
            "where level = #{level} " +
            " <if test=\"pid == null\"> and parentMenuId is null </if> " +
            " <if test=\"pid != null\"> and parentMenuId =#{pid}  </if> " +
            "order by priority asc limit 1 " +
            "" +
            "</script>")
    Menu findTopOneMenu(@Param("level") Integer level,@Param("pid") Integer parentMenuId);

    @Select("<script> " +
            "select * from t_menu " +
            " where priority &lt; #{priority} and level = #{level} " +
            " <if test=\"pid == null\"> and parentMenuId is null </if> " +
            " <if test=\"pid != null\"> and parentMenuId = #{pid} </if>  " +
            " order by priority desc  limit 1 " +
            "</script>")
    Menu findBeforeMenu(@Param("priority") Integer priority, @Param("level")Integer level, @Param("pid")Integer parentMenuId);
}
